package dao.impl;

import bean.Emp;
import dao.EmpDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.DruidUtils;

import java.util.List;

public class EmpDaoImpl implements EmpDao {
    private JdbcTemplate jt = new JdbcTemplate(DruidUtils.ds);

    @Override
    public int update(Integer empno, Emp emp) {
        String sql = "update emp set empno = ?,ename = ?,job = ?,mgr = ?,hiredate = ?,sal = ?,comm = ?,deptno = ? where empno = ?";
        return jt.update(sql,emp.getEmpno(),emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno(),empno);
    }

    @Override
    public int updateEname(Integer empno, String name) {
        String sql = "update emp set ename = ? where empno = ?";
        return jt.update(sql, name, empno);
    }

    @Override
    public int delete(Emp emp) {
        String sql = "delete from emp where empno = ?";
        return jt.update(sql,emp.getEmpno());
    }

    @Override
    public int deleteByEmpno(Integer empno) {
        String sql = "delete from emp where empno = ?";
        return jt.update(sql,empno);
    }

    @Override
    public int deleteByEname(String name) {
        String sql = "delete from emp where ename = ?";
        return jt.update(sql,name);
    }

    @Override
    public int insert(Emp emp) {
        String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
        return jt.update(sql,emp.getEmpno(),emp.getEname(),emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno());
    }

    @Override
    public Emp select(Emp emp) {
        String sql = "select * from emp where empno = ?";
        List<Emp> query = jt.query(sql, new BeanPropertyRowMapper<>(Emp.class), emp.getEmpno());
        return query.get(0);
    }

    @Override
    public Emp selectByEmpno(Integer empno) {
        String sql = "select * from emp where empno = ?";
        List<Emp> query = jt.query(sql, new BeanPropertyRowMapper<>(Emp.class), empno);
        return query.get(0);
    }

    @Override
    public Emp selectByEname(String ename) {
        String sql = "select * from emp where ename = ?";
        List<Emp> query = jt.query(sql, new BeanPropertyRowMapper<>(Emp.class), ename);
        return query.get(0);
    }

    @Override
    public List<Emp> selectAll() {
        String sql = "select * from emp";
        List<Emp> query = jt.query(sql, new BeanPropertyRowMapper<>(Emp.class));
        return query;
    }
}
